//  ========== 
//  = 宾和   登录业务JS = 
//  ========== 
var token;
var Main = function() {
	mui.init({
		swipeBack: false //启用右滑关闭功能
	});
	this.init();
	this.even();
	this.loadData();
};
Main.prototype = {
	init: function() {
		var telphone = Util.requestURL('telphone');
		if(telphone) {
			$("#loginname").val(telphone);
		}

		function guid() {
			function s4() {
				return Math.floor((1 + Math.random()) * 0x10000)
					.toString(16)
					.substring(1);
			}
			return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
				s4() + '-' + s4() + s4() + s4();
		}
		token = guid();
	},
	even: function() {
		/**跳转注册页面 */
		$('#btn-toRegister').on('tap', function() {
			window.location.href = 'register.html';
		});
		/**跳转忘记密码页面 */
		$('#btn-toForgetPwd').on('tap', function() {
			window.location.href = 'forgetPasswordCheckPhone.html'
		});

		function refreshimg() {
			$("#imgCode").val("");
			var url = server_url + "/getCode.do";
			$('#btn-sendImg').attr("src", url + "?token=" + token + "&d=" + Math.random());
		}
		/**刷新图片验证码*/
		$('#btn-sendImg').on('tap', refreshimg);

		function checkPhone(phone) {
			if(!phone) {
				mui.toast("请输入手机号");
				return false;
			}
			if(!Verify.phone(phone)) {
				mui.toast(Verify.phoneMsg);
				return false;
			}
			return true;
		}

		function checkPassword(password) {
			if(!password) {
				mui.toast("请输入密码");
				return false;
			}
			if(!Verify.loginPassword(password)) {
				mui.toast("密码格式不正确");
				return false;
			}
			return true;
		}

		/**登录提交 */
		$('#btn-login').on('tap', function() {
			var loginname = document.getElementById("loginname").value;
			var password = document.getElementById("password").value;
			if(!checkPhone(loginname) || !checkPassword(password)) {
				return false;
			}
			var imgCode = document.getElementById("imgCode").value;
			if(!imgCode || imgCode.length != 4) {
				Util.toast("请输入验证码");
				return false;
			}

			var loginUrl = server_url + "/tologin.do";
			var param = {
				isMobile: isMobile,
				loginname: loginname,
				password: password,
				token: token,
				imgCode: imgCode
			}
			Util.loading();
			$.getJSON(loginUrl, param, function(retData) {
				if(!retData.data && retData.errMsg != "") {
					mui.toast(retData.errMsg);
					Util.closeLoading();
					refreshimg();
					return false;
				}
				window.sessionStorage.clear();
				storage.clear();
				window.sessionStorage.setItem('userInfo', retData);
				storage.setItem("memberId", retData.data.id);
				storage.setItem("loginname", retData.data.loginname);
				storage.setItem("truename", !retData.data.truename ? "" : retData.data.truename);
				storage.setItem("telphone", !retData.data.telphone ? "" : retData.data.telphone);
				storage.setItem("cardcode", !retData.data.cardcode ? "" : retData.data.cardcode);
				storage.setItem("email", !retData.data.email ? "" : retData.data.email);
				storage.setItem("recommandPerson", !retData.data.recommandPerson ? "" : retData.data.recommandPerson); //我的推荐人
				storage.setItem('plainpassword', retData.data.plainpassword); //我的推荐码
				openTab("main.html", 'main');
			});
		});
	},
	loadData: function() {
		var url = server_url + "/getCode.do";
		$('#btn-sendImg').attr("src", url + "?token=" + token);
	}
};